package driver

import (
	"github.com/gin-gonic/gin"
	v1 "golang-sgg-daijia/api/v1"
	"golang-sgg-daijia/internal/handler"
	"golang-sgg-daijia/internal/service"
	"net/http"
	"strconv"
)

type LocationHandler struct {
	*handler.Handler
	locationService service.LocationService
}

func NewLocationHandler(
    handler *handler.Handler,
    locationService service.LocationService,
) *LocationHandler {
	return &LocationHandler{
		Handler:      handler,
		locationService: locationService,
	}
}

// UpdateDriverLocation 开启接单服务：更新司机经纬度位置
func (h *LocationHandler) UpdateDriverLocation(ctx *gin.Context) {
	driverID := ctx.GetInt64("ID")
	req := new(v1.UpdateDriverLocationForm)
	if err := ctx.ShouldBindJSON(req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, nil)
		return
	}
	req.DriverID = driverID
	flag, err := h.locationService.UpdateDriverLocation(ctx, req)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}
	if err == nil && !flag {
		v1.HandleError(ctx, v1.StatusServiceErr, v1.ErrNoStartService, nil)
		return
	}
	v1.HandleSuccess(ctx, flag)
}

// UpdateOrderLocationToCache 司机赶往代驾起始点：更新订单位置到Redis缓存
func (h *LocationHandler) UpdateOrderLocationToCache(ctx *gin.Context) {
	req := new(v1.UpdateOrderLocationForm)
	if err := ctx.ShouldBindJSON(req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, nil)
		return
	}
	flag, err := h.locationService.UpdateOrderLocationToCache(ctx, req)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}
	v1.HandleSuccess(ctx, flag)
}

// SaveOrderServiceLocation 开始代驾服务：保存代驾服务订单位置
func (h *LocationHandler) SaveOrderServiceLocation(ctx *gin.Context) {
	var req []*v1.SaveOrderServiceLocationForm
	if err := ctx.ShouldBindJSON(&req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, nil)
		return
	}
	flag, err := h.locationService.SaveOrderServiceLocation(ctx, req)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}
	v1.HandleSuccess(ctx, flag)
}

// GetOrderServiceLastLocation 代驾服务：获取订单服务最后一个位置信息
func (h *LocationHandler) GetOrderServiceLastLocation(ctx *gin.Context) {
	orderIDStr := ctx.Param("orderId")
	orderID, err := strconv.Atoi(orderIDStr)
	if err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, nil)
		return
	}
	orderSerLastLocVo, err := h.locationService.GetOrderServiceLastLocation(ctx, int64(orderID))
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}
	v1.HandleSuccess(ctx, orderSerLastLocVo)
}



// GetCacheOrderLocation 司机赶往代驾起始点：获取订单经纬度位置
func (h *LocationHandler) GetCacheOrderLocation(ctx *gin.Context) {
	orderIDStr := ctx.Param("orderId")
	orderID, err := strconv.Atoi(orderIDStr)
	if err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, nil)
		return
	}
	orderLocation, err := h.locationService.GetCacheOrderLocation(ctx, int64(orderID))
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}
	v1.HandleSuccess(ctx, orderLocation)
}